
<!DOCTYPE HTML>
<html lang="en" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>读写回调 · smartboot 中文指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="7-AioSession.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    关于smartboot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    smart-socket 使用手册
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../quickStart.html">
            
                <a href="../quickStart.html">
            
                    
                    快速上手
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" >
            
                <span>
            
                    
                    源码篇
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.3.1" data-path="1-ready.html">
            
                <a href="1-ready.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.2" data-path="2-core-interface.html">
            
                <a href="2-core-interface.html">
            
                    
                    核心接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.3" data-path="3-enum.html">
            
                <a href="3-enum.html">
            
                    
                    枚举状态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.4" data-path="4-IoServerConfig.html">
            
                <a href="4-IoServerConfig.html">
            
                    
                    服务配置IoServerConfig
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.5" data-path="5-AioQuickServer.html">
            
                <a href="5-AioQuickServer.html">
            
                    
                    服务端AioQuickServer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.6" data-path="6-AioQuickClient.html">
            
                <a href="6-AioQuickClient.html">
            
                    
                    客户端AioQuickClient
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3.7" data-path="7-AioSession.html">
            
                <a href="7-AioSession.html">
            
                    
                    通信会话AioSession
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3.8" data-path="8-CompletionHandler.html">
            
                <a href="8-CompletionHandler.html">
            
                    
                    读写回调
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.4" >
            
                <span>
            
                    
                    进阶篇
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.4.1" data-path="../second/readme.html">
            
                <a href="../second/readme.html">
            
                    
                    开篇概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.2" data-path="../second/2-decoder.html">
            
                <a href="../second/2-decoder.html">
            
                    
                    协议解码器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.3" data-path="../second/3-type-one.html">
            
                <a href="../second/3-type-one.html">
            
                    
                    一类消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.4" data-path="../second/4-type-two.html">
            
                <a href="../second/4-type-two.html">
            
                    
                    二类消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4.5" data-path="../second/5-type-three.html">
            
                <a href="../second/5-type-three.html">
            
                    
                    三类消息
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.5" >
            
                <a target="_blank" href="https://smartboot.github.io/smart-socket/apidocs/index.html">
            
                    
                    API Docs
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    smart-ioc 使用手册
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    smart-boot 使用手册
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    mydalgen 使用手册
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    技术分享
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../../share/rpc/smart-socket-rpc.html">
            
                <a href="../../share/rpc/smart-socket-rpc.html">
            
                    
                    smart-socket实现RPC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../../share/exception/readme.html">
            
                <a href="../../share/exception/readme.html">
            
                    
                    Exception杂谈
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../../donation.html">
            
                <a href="../../donation.html">
            
                    
                    关于捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >读写回调</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x516B;&#x3001;&#x8BFB;&#x5199;&#x56DE;&#x8C03;">&#x516B;&#x3001;&#x8BFB;&#x5199;&#x56DE;&#x8C03;</h1>
<h3 id="&#x8BFB;&#x56DE;&#x8C03;readcompletionhandler">&#x8BFB;&#x56DE;&#x8C03;ReadCompletionHandler</h3>
<pre><code>class ReadCompletionHandler&lt;T&gt; implements CompletionHandler&lt;Integer, AioSession&lt;T&gt;&gt; {
    private static final Logger LOGGER = LogManager.getLogger(ReadCompletionHandler.class);

    @Override
    public void completed(final Integer result, final AioSession&lt;T&gt; aioSession) {
        // &#x63A5;&#x6536;&#x5230;&#x7684;&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x9884;&#x5904;&#x7406;
        for (Filter h : aioSession.getServerConfig().getFilters()) {
            h.readFilter(aioSession, result);
        }
        aioSession.readFromChannel(result == -1);
    }

    @Override
    public void failed(Throwable exc, AioSession&lt;T&gt; aioSession) {
        if (exc instanceof IOException) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(&quot;session:{} will be closed,msg:{}&quot;, aioSession.getSessionID(), exc.getMessage());
            }
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(&quot;smart-socket read fail:&quot;, exc);
            }
        }

        try {
            aioSession.getServerConfig().getProcessor().stateEvent(aioSession, StateMachineEnum.INPUT_EXCEPTION, exc);
        } catch (Exception e) {
            LOGGER.catching(e);
        }
        try {
            aioSession.close();
        } catch (Exception e) {
            LOGGER.catching(e);
        }
    }
}
</code></pre><p>&#x6267;&#x884C;&#x8BFB;&#x56DE;&#x8C03;&#x662F;&#x4E3A;&#x4E86;&#x5904;&#x7406;&#x5F53;&#x524D;&#x4ECE;&#x7F51;&#x7EDC;&#x4E0A;&#x8BFB;&#x53D6;&#x5230;&#x7684;&#x5B57;&#x8282;&#x6D41;&#xFF0C;&#x5728;AIO&#x901A;&#x4FE1;&#x4E2D;&#x88AB;&#x5C01;&#x88C5;&#x4E3A;ByteBuffer&#x5BF9;&#x8C61;&#x3002;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x89E3;&#x6790;&#xFF0C;&#x8FD8;&#x539F;&#x6210;&#x6D88;&#x606F;&#x5B9E;&#x4F53;&#x5E76;&#x8FDB;&#x884C;&#x4E1A;&#x52A1;&#x5904;&#x7406;&#x3002;
&#x6EE1;&#x8DB3;&#x4EE5;&#x4E0B;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x6761;&#x4EF6;&#x65F6;&#xFF0C;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;&#x8BFB;&#x56DE;&#x8C03;&#xFF1A;</p>
<ol>
<li>&#x4ECE;&#x7F51;&#x7EDC;&#x4E0A;&#x8BFB;&#x53D6;&#x5230;&#x65B0;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x81F3;&#x5C11;1&#x4E2A;&#x5B57;&#x8282;&#x3002;</li>
<li>&#x5BF9;&#x7AEF;&#x670D;&#x52A1;&#x5B8C;&#x6210;&#x8F93;&#x51FA;&#x5E76;&#x5173;&#x95ED;write&#x901A;&#x9053;&#xFF0C;&#x5373;&#x672C;&#x901A;&#x9053;&#x7684;read&#x64CD;&#x4F5C;&#x5DF2;&#x7EC8;&#x6B62;&#xFF0C;&#x6B64;&#x65F6;<code>result==-1</code>&#x3002;&#x5982;&#x56FE;&#x6240;&#x793A;&#xFF1A;
<img src="https://static.oschina.net/uploads/img/201802/10115533_GGM4.png" alt="&#x8F93;&#x5165;&#x56FE;&#x7247;&#x8BF4;&#x660E;" title="&#x5728;&#x8FD9;&#x91CC;&#x8F93;&#x5165;&#x56FE;&#x7247;&#x6807;&#x9898;"></li>
</ol>
<p>&#x5F53;&#x670D;&#x52A1;&#x5728;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x8BFB;&#x53D6;&#x3001;&#x6216;&#x8005;&#x8BFB;&#x56DE;&#x8C03;&#x5904;&#x7406;&#x8FC7;&#x7A0B;&#x4E2D;&#x51FA;&#x73B0;&#x5F02;&#x5E38;&#x65F6;&#xFF0C;&#x4F1A;&#x89E6;&#x53D1;ReadCompletionHandler&#x7684;failed&#x65B9;&#x6CD5;&#x3002;&#x6846;&#x67B6;&#x4F1A;&#x89E6;&#x53D1;StateMachineEnum.INPUT_EXCEPTION&#x72B6;&#x6001;&#x673A;&#xFF0C;&#x4E1A;&#x52A1;&#x81EA;&#x884C;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x5904;&#x7406;&#x3002;&#x4E3A;&#x4E86;&#x89C4;&#x907F;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x5FFD;&#x7565;&#x6B64;&#x7C7B;&#x5F02;&#x5E38;&#x5BFC;&#x81F4;&#x8D44;&#x6E90;&#x65E0;&#x6CD5;&#x91CA;&#x653E;&#xFF0C;smart-socket&#x4F1A;&#x4E3B;&#x52A8;&#x6267;&#x884C;AioSession.close&#x3002;</p>
<hr>
<h3 id="&#x5199;&#x56DE;&#x8C03;writecompletionhandler">&#x5199;&#x56DE;&#x8C03;WriteCompletionHandler</h3>
<pre><code>class WriteCompletionHandler&lt;T&gt; implements CompletionHandler&lt;Integer, AioSession&lt;T&gt;&gt; {
    private static final Logger LOGGER = LogManager.getLogger(WriteCompletionHandler.class);

    @Override
    public void completed(final Integer result, final AioSession&lt;T&gt; aioSession) {
        // &#x63A5;&#x6536;&#x5230;&#x7684;&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x9884;&#x5904;&#x7406;
        for (Filter h : aioSession.getServerConfig().getFilters()) {
            h.writeFilter(aioSession, result);
        }
        aioSession.writeToChannel();
    }

    @Override
    public void failed(Throwable exc, AioSession&lt;T&gt; aioSession) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(&quot;smart-socket write fail:&quot;, exc);
        }
        try {
            aioSession.getServerConfig().getProcessor().stateEvent(aioSession, StateMachineEnum.OUTPUT_EXCEPTION, exc);
        } catch (Exception e) {
            LOGGER.catching(e);
        }
        try {
            aioSession.close();
        } catch (Exception e) {
            LOGGER.catching(e);
        }
    }
}
</code></pre><p>&#x4E1A;&#x52A1;&#x5B9E;&#x73B0;&#x4E2D;&#x4F1A;&#x901A;&#x8FC7;AioSession.write&#x7ED9;&#x4E88;&#x5BF9;&#x7AEF;&#x54CD;&#x5E94;&#x6D88;&#x606F;&#xFF0C;&#x8FD9;&#x4E2A;write&#x64CD;&#x4F5C;&#x662F;&#x975E;&#x963B;&#x585E;&#x7684;&#x3002;&#x5F53;&#x6570;&#x636E;&#x8F93;&#x51FA;&#x540E;&#x4F1A;&#x4EE5;&#x5199;&#x56DE;&#x8C03;WriteCompletionHandler&#x7684;&#x65B9;&#x5F0F;&#x544A;&#x77E5;&#x6846;&#x67B6;&#xFF0C;&#x5DF2;&#x7ECF;&#x6709;&#x4E00;&#x90E8;&#x5206;&#x6570;&#x636E;&#x6210;&#x529F;&#x5199;&#x51FA;&#x53BB;&#x4E86;&#x3002;&#x6BCF;&#x6B21;&#x89E6;&#x53D1;&#x56DE;&#x8C03;&#x540E;&#x90FD;&#x9700;&#x8981;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x8FD8;&#x6709;&#x5F85;&#x8F93;&#x51FA;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x82E5;&#x6709;&#x5219;&#x7EE7;&#x7EED;&#x6267;&#x884C;write&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x7B49;&#x5F85;&#x4E0B;&#x4E00;&#x6B21;&#x5199;&#x56DE;&#x8C03;&#x3002;&#x81EA;&#x6B64;&#x6570;&#x636E;&#x8F93;&#x51FA;&#x64CD;&#x4F5C;&#x4FBF;&#x5F62;&#x5F0F;&#x4E00;&#x4E2A;&#x5065;&#x5EB7;&#x7684;&#x6267;&#x884C;&#x95ED;&#x73AF;&#x3002;
&#x5F53;&#x7136;&#x8FD9;&#x4E2A;&#x8F93;&#x51FA;&#x7684;&#x8FC7;&#x7A0B;&#x4F1A;&#x5982;&#x540C;&#x5199;&#x64CD;&#x4F5C;&#x4E00;&#x6837;&#x51FA;&#x73B0;&#x4E0D;&#x53EF;&#x9884;&#x77E5;&#x7684;&#x5F02;&#x5E38;&#xFF0C;&#x6B64;&#x65F6;&#x540C;&#x6837;&#x4F1A;&#x901A;&#x8FC7;WriteCompletionHandler.failed&#x65B9;&#x6CD5;&#x89E6;&#x53D1;&#x72B6;&#x6001;&#x673A;StateMachineEnum.OUTPUT_EXCEPTION&#xFF0C;&#x6700;&#x540E;&#x6846;&#x67B6;&#x81EA;&#x52A8;&#x5173;&#x95ED;&#x8FDE;&#x63A5;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; smartboot.org 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-05-27 14:34:42
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="7-AioSession.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 通信会话AioSession">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"读写回调","level":"1.2.3.8","depth":3,"next":{"title":"进阶篇","level":"1.2.4","depth":2,"ref":"","articles":[{"title":"开篇概述","level":"1.2.4.1","depth":3,"path":"smart-socket/second/readme.md","ref":"smart-socket/second/readme.md","articles":[]},{"title":"协议解码器","level":"1.2.4.2","depth":3,"path":"smart-socket/second/2-decoder.md","ref":"smart-socket/second/2-decoder.md","articles":[]},{"title":"一类消息","level":"1.2.4.3","depth":3,"path":"smart-socket/second/3-type-one.md","ref":"smart-socket/second/3-type-one.md","articles":[]},{"title":"二类消息","level":"1.2.4.4","depth":3,"path":"smart-socket/second/4-type-two.md","ref":"smart-socket/second/4-type-two.md","articles":[]},{"title":"三类消息","level":"1.2.4.5","depth":3,"path":"smart-socket/second/5-type-three.md","ref":"smart-socket/second/5-type-three.md","articles":[]}]},"previous":{"title":"通信会话AioSession","level":"1.2.3.7","depth":3,"path":"smart-socket/first/7-AioSession.md","ref":"smart-socket/first/7-AioSession.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","search-plus","expandable-chapters-interactive","tbfed-pagefooter","github","github-buttons","edit-link"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy smartboot.org 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/smartboot/smart-socket"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"expandable-chapters-interactive":{},"github-buttons":{"buttons":[{"user":"smartboot","repo":"smart-socket","type":"star","size":"small","count":true}]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/smartboot/docs/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"smartboot 中文指南","language":"en","gitbook":"*","description":"本文涵盖了smartboot下所有项目的技术手册"},"file":{"path":"smart-socket/first/8-CompletionHandler.md","mtime":"2018-05-27T06:34:42.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-23T08:59:34.607Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-interactive/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

